/**
 * 功能描述
 */

define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var HandlerBars = require("handlebars");	
	var roomTpl = require("../assets/tpl/roomstatewhole_room.tpl");
	var appointRoomBarComp = require("../component/appointRoomBarComp");
    module.exports = {
		setData:function(status,widget){
    		var subnav=widget.get("subnav");
    		var startDate = moment().startOf("day").valueOf();
    		var endDate = moment().endOf("day").valueOf();    		
    		aw.ajax({
    			url:"api/room/nowpanorama",
    			data:{    				
					start: startDate,
					end: endDate,
					status:status,
					fetchProperties:"floor,roomlist.*"
				},
				success: function(data){					
					var result = widget.store.buildingData(data);
					var html = HandlerBars.compile(roomTpl)(result);
		        	$(".J-room",widget.element).html(html);
		        	
				}
    		});
    		aw.ajax({
    			url:"api/room/statistics",
    			data:{
    				all:true,
					start: startDate,
					end: endDate,
				},
				success: function(ret){
					var num = ret[0];
					widget.renderRoomNumber(num);
				}
    		});
    	},		
    	renderRoomNumber:function(data){
    		
    		$(".el-roomTypes-all>.room-code>span").text(data.allRoomCount? data.allRoomCount : 0);
    		$(".el-roomTypes-clear>.room-code>span").text(data.clear ? data.clear : 0);
    		$(".el-roomTypes-dirty>.room-code>span").text(data.dirty ? data.dirty : 0);
    		$(".el-roomTypes-checkin>.room-code>span").text(data.checkIn ? data.checkIn : 0);
    		$(".el-roomTypes-willgo>.room-code>span").text(data.willGo ? data.willGo : 0);
    		$(".el-roomTypes-appoint>.room-code>span").text(data.appoint ? data.appoint : 0);
    		$(".el-roomTypes-repairing>.room-code>span").text(data.repairing ? data.repairing : 0);
    		$(".el-roomTypes-temporary>.room-code>span").text(data.temporary ? data.temporary : 0);
    		$(".el-roomTypes-selfuse>.room-code>span").text(data.selfUse ? data.selfUse : 0);
    		$(".el-roomTypes-free>.room-code>span").text(data.free ? data.free : 0);
    	},
    	clickRoom:function(e,widget){
    		var pkRoom = $(e.currentTarget).attr("data-pkRoom");
    		var state = JSON.parse($(e.currentTarget).attr("data-state"));
    		var form = widget.get("form");
    		var grid = widget.get("grid");
    		aw.ajax({
    			url:"api/roomregister/querybypkroom",
    			data:{
    				pkRoom:pkRoom,
    				fetchProperties:"*,orgBuilding.*,roomType.pkRoomType,roomType.name"
				},
				success: function(data){
					if(data.dataSource=="GroupReserve"){
						widget.get("subnav").hide(["nonmemcheckout"]);
					}else{
						widget.get("subnav").hide(["checkoutsettlement"]);
					}
					form.setData(data);
					widget.set("pkCIImplement",data.pkCIImplement);
					widget.set("statementStatus",data.statementStatus);
					widget.set("checkOutStatus",data.checkOutStatus);
					if(data.budName==null || data.code==null){
						aw.ajax({
			    			url:"api/room/query",
			    			data:{
			    				pkRoom:pkRoom,
			    				fetchProperties:"*,orgBuilding.*,roomType.pkRoomType,roomType.name"
							},
							success:function(data){
								form.setData(data[0]);
								form.setValue("budName",data[0].orgBuilding.name);
								form.setValue("orientation",data[0].orientation.value);
								form.setValue("rmtName",data[0].roomType.name);
								form.setValue("bedcount",data[0].bedcount==null?0:data[0].bedcount);
								form.setValue("rStatus",data[0].status.value);
							}
						});
					}
				}
    		});
    		for(var i=0;i<state.length;i++){
    			if(state[i].key == "CheckIn"){
    				aw.ajax({
    	    			url:"api/roompersonalInfo/query",
    	    			data:{
    	    				"roomRegister.room.pkRoom":pkRoom,
    	    				fetchProperties:"pkRoomPersonalInfo,personalInfo.mobilePhone,personalInfo.name,personalInfo.sex.*,personalInfo.idNumber," +
    	    						"nonMemberRegister.signUpRegister.*,checkInImplement.statementStatus.*," +
    	    						"nonMemberRegister.signUpRegister.personalInfo.*,nonMemberRegister.signUpRegister.personalInfo.sex.*," +
    	    						"signUpRegister.*,signUpRegister.personalInfo.*,signUpRegister.personalInfo.sex.*"
    					},
    					success: function(data){
    						if(data && data.length>0){
    							var sdata = [];
    							for(var i=0; i < data.length; i++){
    								// 会员团
    								if(data[i].signUpRegister){
    									if(data[i].signUpRegister.checkOut 
    											&& data[i].checkInImplement.statementStatus
    											&& data[i].checkInImplement.statementStatus.key == "Statement"){
    										continue;
    									}else{
    										data[i].personalInfo = data[i].signUpRegister.personalInfo;
    									}
    								}else{
    									// 散客团
    									if(data[i].nonMemberRegister.signUpRegister.checkOut 
    											&& data[i].checkInImplement.statementStatus
    											&& data[i].checkInImplement.statementStatus.key == "Statement"){
    										continue;
    									}else{
    										data[i].personalInfo = data[i].nonMemberRegister.signUpRegister.personalInfo;
    									}
    								}
    								sdata.push(data[i]);
    							}
    							grid.setData(sdata);
        						widget.show([".J-grid"]);
    						}
    					}
    	    		});
    				widget.get("subnav").show(["nonmemcheckout","checkoutsettlement"]);
    			}else if(state[i].key == "WillGo"){
    				widget.get("subnav").show(["nonmemcheckout","checkoutsettlement"]);
    			}else if(state[i].key == "Clear" || state[i].key == "Appoint"){
    				widget.get("subnav").show(["scattercheckin","checkin"]);
    			}
    		}
    		
    		widget.get("subnav").hide(["toDetail","appointSum","print"]).show(["return"]);
			widget.show([".J-form"]).hide([".J-status",".J-room",".J-appointRoom"]);
			
    	},
    	appointSumEvent:function(params,widget){
    		widget.toBar();
    		aw.ajax({
    			url:"api/roomstatistics/queryorderroom",
    			data:{
    				fetchProperties:"day,roomCount"
				},
				success: function(data){
	            	appointRoomBarComp(data, widget);
				}
    		});
    	}
    	
    };
});
